package com.wanho.java156.controller;

import com.github.pagehelper.PageInfo;
import com.wanho.java156.po.Order;
import com.wanho.java156.po.Product;
import com.wanho.java156.po.Role;
import com.wanho.java156.po.User;
import com.wanho.java156.service.RoleService;
import com.wanho.java156.service.UserService;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserControlller {
    private static final Logger LOGGER = Logger.getLogger(UserControlller.class) ;
    @Autowired
    private UserService userService ;
    @Autowired
    private RoleService roleService ;
    @Value("${oss.locationUrl}")
    private String imageLocationUrl ;

    @Value("${oss.imageStyle}")
    private String imageStyle ;

    /**
     * 管理员登录
     * @param username  账户
     * @param password  密码
     * @param rememberMe  是否记住我？
     * @return
     */
    @PostMapping("/login")
    public ModelAndView login(String username,
                              String password,
                              @RequestParam(required = false,defaultValue = "false") boolean rememberMe){
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        ModelAndView modelAndView = new ModelAndView();
        try {
            //设置记住密码
            token.setRememberMe(rememberMe);
            subject.login(token);
            modelAndView.setViewName("pages/main");
            LOGGER.debug("imageLocationUrl========="+imageLocationUrl);
            LOGGER.debug("imageStyle========="+imageStyle);
            modelAndView.addObject("imageLocationUrl",imageLocationUrl) ;
            modelAndView.addObject("imageStyle",imageStyle) ;
        } catch (AuthenticationException e) {
            e.printStackTrace();
            modelAndView.setViewName("failer");
        }
        return modelAndView ;
    }

    @GetMapping("/list")
    public ModelAndView list(
            @RequestParam(name = "page",required = false,defaultValue = "1") int pageNum,
            @RequestParam(name = "size",required = false,defaultValue = "5") int pageSize,
            @RequestParam(required = false,defaultValue = "") String queryStr){
        PageInfo<User> pageInfo = userService.list(pageNum,pageSize,queryStr);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/user-list");
        modelAndView.addObject("pageInfo",pageInfo) ;
        return modelAndView ;
    }

    @PostMapping("/add")
    public ModelAndView add(User user){
        boolean save = userService.save(user) ;
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/user/list");
        return modelAndView ;
    }

    @GetMapping("/view")
    public ModelAndView view(String userId){
        User user = userService.findById(userId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/user-show");
        modelAndView.addObject("user",user) ;
        return  modelAndView ;
    }

    @GetMapping("/findUserByIdAndAllRole")
    public ModelAndView findUserByIdAndAllRole(String userId){
        User user = userService.findById(userId);
        List<Role> userRoleList = user.getRoles();
        StringBuffer roleIds = new StringBuffer();
        userRoleList.forEach(role -> roleIds.append(role.getId()).append(","));
        List<Role> roleList = roleService.selectAllRoleAndPerm();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/user-role-add");
        modelAndView.addObject("user",user) ;
        modelAndView.addObject("roleIds",roleIds) ;
        modelAndView.addObject("roleList",roleList) ;
        return  modelAndView ;
    }

    /**
     * 用户关联角色
     * @param userId
     * @param roleIds
     * @return
     */
    @PostMapping("/addRoleToUser")
    public ModelAndView addRoleToUser(String userId,String[] roleIds){
        userService.addRoleToUser(userId,roleIds) ;
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/user/list");
        return  modelAndView ;
    }
}
